package com.liuxingzhiwu.ruijishangcheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxingzhiwu.ruijishangcheng.dto.SetmealDto;
import com.liuxingzhiwu.ruijishangcheng.entity.DishFlavor;
import com.liuxingzhiwu.ruijishangcheng.entity.Setmeal;
import com.liuxingzhiwu.ruijishangcheng.entity.SetmealDish;
import com.liuxingzhiwu.ruijishangcheng.mapper.SetmealMapper;
import com.liuxingzhiwu.ruijishangcheng.service.SetmealDishService;
import com.liuxingzhiwu.ruijishangcheng.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveWithSetmealDish(HttpServletRequest request, SetmealDto setmealDto) {
        Long empId = (Long) request.getSession().getAttribute("employee");

        //1、添加套餐
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setCreateUser(empId);
        setmealDto.setUpdateUser(empId);
        this.save(setmealDto);

        //2、添加菜品菜品（菜品有多中是一个List数组）
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //在套餐菜品里面添加菜品id
        //将集合里面的元素取出来添加id等数据，在返回集合
        setmealDishes = setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            item.setCreateTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            item.setCreateUser(empId);
            item.setUpdateUser(empId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void updateWithSetmealDish(HttpServletRequest request, SetmealDto setmealDto) {
        Long empId = (Long) request.getSession().getAttribute("employee");

        //1、更新套餐
        setmealDto.setUpdateUser(empId);
        setmealDto.setUpdateTime(LocalDateTime.now());

        this.updateById(setmealDto);

        //2、更新套餐菜品
        //根据套餐id删除原来的套餐菜品
        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id",setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //将集合里面的元素取出来添加id等数据，在返回集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            item.setCreateTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            item.setCreateUser(empId);
            item.setUpdateUser(empId);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }
}
